package com.qq.xqj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.xqj.modle.Order_info;
import com.qq.xqj.service.Order_service;
import com.qq.xqj.service.impl.Order_serviceImpl;
import com.qq.xqj.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order")
public class Order_controller {

    @Autowired
    Order_serviceImpl order_serviceimpl;

    //分页展示主页面
    @GetMapping("/pageOrder")
    public String getALLOrder(Model m, @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum){
        PageHelper.startPage(pageNum,6);
        List<Order_info> order_info = order_serviceimpl.getALLOrder();
        System.out.println("订单信息"+order_info);
        PageInfo<Order_info> order_infoPageInfo = new PageInfo<>(order_info);
        System.out.println("查询全部"+order_infoPageInfo);
        m.addAttribute("PageInfo",order_infoPageInfo);
        return "xqj/order1";
    }

    //删除一条数据
    @RequestMapping("/deleteOne")
    @ResponseBody
    public ResultInfo deleteOrderById(String orderId){
        int i = order_serviceimpl.deleteOrderById(orderId);
        System.out.println(orderId);
        if (i != 0 ){
            return new ResultInfo("200","删除成功");
        }
        else {
            return new ResultInfo("500","删除失败");
        }
    }

    //批量删除
    @RequestMapping("/deleteMany")
    public String deleteMany(HttpServletRequest request) {
        String[] ids = request.getParameterValues("id[]");
        if (ids != null){
        for (String orderId : ids) {
            order_serviceimpl.deleteOrderById(orderId);
        }
        }
        return "redirect:pageOrder";
    }



    //通过ID得到数据，跳转到修改页面
    @RequestMapping("/updateOrder/{orderId}")
    public String updateById(@PathVariable Integer orderId , Model m1){
        System.out.println(orderId);
        m1.addAttribute("orders",order_serviceimpl.queryOrderById(orderId));
        return "xqj/updateOrder";
    }

    //通过ID修改订单
    @RequestMapping("/updateOrderById")
    @ResponseBody
    public ResultInfo updateOrderById(String orderId,String brand,String type,String color,String sellingPrice,String salesmanName,String salesmanId,String payment,String royalty){
        Order_info o = new Order_info();
        o.setOrderId(orderId);
        o.setBrand(brand);
        o.setType(type);
        o.setColor(color);
        o.setSellingPrice(sellingPrice);
        o.setSalesmanName(salesmanName);
        o.setSalesmanId(salesmanId);
        o.setPayment(payment);
        o.setRoyalty(royalty);
        System.out.println(o);

        int i = order_serviceimpl.updateOrderById(o);
        if (i != 0 ){
            return new ResultInfo("200","修改成功");
        }
        else {
            return new ResultInfo("500","修改失败");
        }
    }

    //跳转到新增页面
    @RequestMapping("/addPage")
    public String addPage(){
        return "xqj/add";
    }


    //新增订单
    @RequestMapping("/add")
    @ResponseBody
    public ResultInfo addOrder(Order_info order_info){
        try {
            order_serviceimpl.queryTotal(order_info);
        } catch (RuntimeException e) {
            return new ResultInfo("500", "该仓库库存不足，请查询后添加");
        }
        int i = order_serviceimpl.addOrder(order_info);
        System.out.println(order_info);
        if (i != 0 ){
            return new ResultInfo("200","添加成功");
        }
        else {
            return new ResultInfo("500","添加失败");
        }
    }

    //条件查询
    @RequestMapping("/selectByKey")
    public String selectByKey(Order_info order_info, @RequestParam(defaultValue = "1",value = "pageMum")Integer pageNum, ModelMap m){
//        if (order_info.getBrand() != null || order_info.getType() != null||order_info.getSalesmanName() !=null||order_info.getSellingTime()!=null||order_info.getPayment()!=null) {
            PageHelper.startPage(pageNum, 8);
        System.out.println("!!!!!!!!!!!!!!!!!!!!"+order_info);
        List<Order_info> list = order_serviceimpl.queryOrderByKey(order_info);
        System.out.println("list = " + list);
            PageInfo<Order_info> order_infoPageInfo = new PageInfo<>(list);
            m.addAttribute("PageInfo", order_infoPageInfo);
            return "xqj/order1";
        }
//        return "redirect:pageOrder";
//
//
//    }





    @GetMapping("/test")
    public String test(){
        return "xqj/add";
    }
}
